package com.example.cephdemo.demos.web;

import com.example.cephdemo.demos.base.Result;
import com.example.cephdemo.demos.request.CompleteMultipartUploadRequest;
import com.example.cephdemo.demos.request.MultipartUploadCreateRequest;
import com.example.cephdemo.demos.service.FileService;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

/**
 * @author winterchen
 * @version 1.0
 * @date 2022/4/21 16:56
 **/
@RequestMapping("/file")
@RestController
@RequiredArgsConstructor
public class FileUploadController {

    private final FileService fileUploadService;

    @PostMapping("/upload/file")
    public Result uploadFile(
            @RequestParam(value = "file", required = true)
            MultipartFile file,
            @RequestParam(name = "pass", required = false)
            String pass,
            @RequestParam(name = "expire", required = true)
            Integer expire,
            @RequestParam(name = "maxGetCount", required = true)
            Integer maxGetCount,
            @RequestParam(name = "bucket", required = true)
            String bucket
    ) {
        return Result.success(fileUploadService.upload(file, bucket));
    }
    @PostMapping("/multipart/create")
    public Result createMultipartUpload(
            @RequestBody
            @Validated
            MultipartUploadCreateRequest multipartUploadCreateRequest
    ) {
        return Result.success(fileUploadService.createMultipartUpload(multipartUploadCreateRequest));
    }

    @PostMapping("/multipart/complete")
    public Result completeMultipartUpload(
            @RequestBody
            @Validated
            CompleteMultipartUploadRequest uploadRequest
    ) {
        return Result.success(fileUploadService.completeMultipartUpload(uploadRequest));
    }

    @DeleteMapping("/{fileName}")
    public void remove(
            @PathVariable("fileName")
            String fileName,
            @RequestParam(name = "bucket", required = true)
            String bucket
    ) {
        fileUploadService.remove(fileName, bucket);
    }
}